package com.anysoft.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.bean.QueryFundListBean;

import java.util.List;

public class FundListNewAdapter extends BaseAdapter {
    public static final int TYPE_TITLE = 0;
    public static final int TYPE_SELECTED = 1;
    public static final int TYPE_NEW = 2;
    private Context context;
    private List<QueryFundListBean.DataBean> selectedList;
    private List<QueryFundListBean.DataBean> newList;

    private OnBuyQuicklyBtnListener onBuyQuicklyBtnListener;

    public void setOnBuyQuicklyBtnListener(OnBuyQuicklyBtnListener onBuyQuicklyBtnListener) {
        this.onBuyQuicklyBtnListener = onBuyQuicklyBtnListener;
    }

    private OnBuyBtnListener onBuyBtnListener;

    public void setOnBuyBtnListener(OnBuyBtnListener onBuyBtnListener) {
        this.onBuyBtnListener = onBuyBtnListener;
    }

    private OnRelativeLayoutClickListener onRelativeLayoutClickListener;

    public void setOnRelativeLayoutListener(OnRelativeLayoutClickListener onRelativeLayoutClickListener) {
        this.onRelativeLayoutClickListener = onRelativeLayoutClickListener;
    }

    public FundListNewAdapter(Context context, List<QueryFundListBean.DataBean> selectedList,
                              List<QueryFundListBean.DataBean> newList) {
        super();
        this.context = context;
        this.selectedList = selectedList;
        this.newList = newList;
    }

    @Override
    public int getCount() {
        if (selectedList == null || selectedList.size() == 0) {
            return 1 + newList.size();
        }

        if (newList == null || newList.size() == 0) {
            return selectedList.size();
        }

        return selectedList.size() + 1 + newList.size();
    }

    @Override
    public Object getItem(int position) {
//        if (position < selectedList.size()) {
//            return selectedList.get(position);
//        } else if (position == selectedList.size()) {
//            return null;
//        } else {
//            return newList.get(position);
//        }
        return null;
    }

    @Override
    public long getItemId(int position) {
//        return position;
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (selectedList == null || selectedList.size() == 0) {
            if (position == 0) {
                return TYPE_TITLE;
            } else {
                return TYPE_NEW;
            }
        }

        if (newList == null || newList.size() == 0) {
            return TYPE_SELECTED;
        }

        if (position < selectedList.size()) {
            return TYPE_SELECTED;
        } else if (position == selectedList.size()) {
            return TYPE_TITLE;
        } else {
            return TYPE_NEW;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TitleViewHolder titleHolder;
        FundSelectedViewHolder fundSelectedViewHolder;
        FundNewViewHolder fundNewViewHolder;
        switch (getItemViewType(position)) {
            case TYPE_TITLE:
                //System.out.println("TYPE_TITLE---" + position);
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_fund_title, null);
                    titleHolder = new TitleViewHolder();
                    convertView.setTag(titleHolder);
                } else {
                    titleHolder = (TitleViewHolder) convertView.getTag();
                }
                break;
            case TYPE_SELECTED:
                //System.out.println("TYPE_SELECTED---" + position);
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_fund_select_again, null);
                    fundSelectedViewHolder = new FundSelectedViewHolder();

                    fundSelectedViewHolder.rl_item_fund_selected = (RelativeLayout) convertView
                            .findViewById(R.id.rl_item_fund_selected);
                    fundSelectedViewHolder.ll_item_fund_selected_circle = (LinearLayout) convertView
                            .findViewById(R.id.ll_item_fund_selected_circle);
                    fundSelectedViewHolder.tv_item_fund_selected_symbol = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_selected_symbol);
                    fundSelectedViewHolder.tv_item_fund_selected_number = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_selected_number);
                    fundSelectedViewHolder.tv_item_fund_selected_time = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_selected_time);
                    fundSelectedViewHolder.tv_item_fund_selected_buy = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_selected_buy);
                    fundSelectedViewHolder.tv_item_fund_selected_fundname = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_selected_fundname);
                    fundSelectedViewHolder.tv_item_fund_selected_fundname_des = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_selected_fundname_des);
                    fundSelectedViewHolder.tv_item_fund_selected_value = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_selected_value);
                    convertView.setTag(fundSelectedViewHolder);
                } else {
                    fundSelectedViewHolder = (FundSelectedViewHolder) convertView.getTag();
                }
                final QueryFundListBean.DataBean bean = selectedList.get(position);

                changeColor(fundSelectedViewHolder.tv_item_fund_selected_symbol, position + 1);
                changeColor(fundSelectedViewHolder.tv_item_fund_selected_number, position + 1);
                changeColor(fundSelectedViewHolder.tv_item_fund_selected_time, position + 1);
                changeColor(fundSelectedViewHolder.tv_item_fund_selected_value, position + 1);
                changeBackGround(fundSelectedViewHolder.ll_item_fund_selected_circle, position + 1);

                fundSelectedViewHolder.tv_item_fund_selected_number.setText(bean.getSelectIncomeValue());
                fundSelectedViewHolder.tv_item_fund_selected_time.setText(bean.getSelectIncome());
                if ("2".equals(bean.getFundtype()) || "9".equals(bean.getFundtype())) {
                    fundSelectedViewHolder.tv_item_fund_selected_value.setText("七日年化 " + bean.getIncomeratio());//incomeratio
                } else {
                    fundSelectedViewHolder.tv_item_fund_selected_value.setText("最新净值(元) " + bean.getPernetvalue());
                }
                fundSelectedViewHolder.tv_item_fund_selected_fundname.setText(bean.getFundname());
                fundSelectedViewHolder.tv_item_fund_selected_fundname_des.setText(bean.getFundIntroduceStr());
                fundSelectedViewHolder.rl_item_fund_selected.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onRelativeLayoutClickListener != null) {
                            onRelativeLayoutClickListener.OnRelativeLayoutClick(bean);
                        }
                    }
                });

                fundSelectedViewHolder.tv_item_fund_selected_buy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onBuyQuicklyBtnListener != null) {
                            onBuyQuicklyBtnListener.OnBuyQuicklyBtnClick(bean);
                        }
                    }
                });
                break;
            case TYPE_NEW:
                //System.out.println("TYPE_NEW---" + position);
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_fund_new_again, null);
                    fundNewViewHolder = new FundNewViewHolder();

                    fundNewViewHolder.rl_item_fund_new = (RelativeLayout) convertView
                            .findViewById(R.id.rl_item_fund_new);
                    fundNewViewHolder.tv_item_fund_new_fundname = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_new_fundname);
                    fundNewViewHolder.tv_item_fund_new_time = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_new_time);
                    fundNewViewHolder.tv_item_fund_new_fundname_des = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_new_fundname_des);
                    fundNewViewHolder.tv_item_fund_new_buy = (TextView) convertView
                            .findViewById(R.id.tv_item_fund_new_buy);
                    convertView.setTag(fundNewViewHolder);
                } else {
                    fundNewViewHolder = (FundNewViewHolder) convertView.getTag();
                }

                //System.out.println("newList--" + newList.size() + ",position---" + (position - selectedList.size() - 1));
                final QueryFundListBean.DataBean newBean = newList.get(position - selectedList.size() - 1);

                fundNewViewHolder.tv_item_fund_new_fundname.setText(newBean.getFundname());
                fundNewViewHolder.tv_item_fund_new_fundname_des.setText(newBean.getFundIntroduceStr());
                if (TextUtils.isEmpty(newBean.getCreateTime())) {
                    fundNewViewHolder.tv_item_fund_new_time.setText("--");
                } else {
                    String[] dates = newBean.getCreateTime().split("-");
                    String date = dates[0] + "年" + dates[1] + "月" + dates[2] + "日起发行";
                    fundNewViewHolder.tv_item_fund_new_time.setText(date);
                }

                fundNewViewHolder.rl_item_fund_new.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onRelativeLayoutClickListener != null) {
                            onRelativeLayoutClickListener.OnRelativeLayoutClick(newBean);
                        }
                    }
                });

                fundNewViewHolder.tv_item_fund_new_buy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onBuyBtnListener != null) {
                            onBuyBtnListener.OnBuyBtnClick(newBean);
                        }
                    }
                });
                break;
        }
        return convertView;
    }

    class TitleViewHolder {
        TextView title;
    }

    class FundNewViewHolder {
        RelativeLayout rl_item_fund_new;
        TextView tv_item_fund_new_fundname;
        TextView tv_item_fund_new_time;
        TextView tv_item_fund_new_fundname_des;
        TextView tv_item_fund_new_buy;
    }

    class FundSelectedViewHolder {
        RelativeLayout rl_item_fund_selected;
        LinearLayout ll_item_fund_selected_circle;
        TextView tv_item_fund_selected_symbol;
        TextView tv_item_fund_selected_number;
        TextView tv_item_fund_selected_time;
        TextView tv_item_fund_selected_buy;
        TextView tv_item_fund_selected_fundname;
        TextView tv_item_fund_selected_fundname_des;
        TextView tv_item_fund_selected_value;
    }

    public interface OnBuyQuicklyBtnListener {

        /**
         * @param bean
         */
        void OnBuyQuicklyBtnClick(QueryFundListBean.DataBean bean);
    }

    public interface OnBuyBtnListener {

        /**
         * @param bean
         */
        void OnBuyBtnClick(QueryFundListBean.DataBean bean);
    }

    public interface OnRelativeLayoutClickListener {

        /**
         * @param bean
         */
        void OnRelativeLayoutClick(QueryFundListBean.DataBean bean);
    }

    private void initTextview(TextView textView, String str) {
        if (TextUtils.isEmpty(str)) {
            textView.setText("--");
        } else {
            if (str.contains("-")) {
                textView.setTextColor(context.getResources().getColor(R.color.black));
                textView.setText(str);
            } else {
                textView.setTextColor(context.getResources().getColor(R.color.red));
                textView.setText(str);
            }
        }
    }

    private void changeColor(TextView textView, int type) {
        switch (type) {
            case 1:
                textView.setTextColor(context.getResources().getColor(R.color.circle_1));
                break;
            case 2:
                textView.setTextColor(context.getResources().getColor(R.color.circle_2));
                break;
            case 3:
                textView.setTextColor(context.getResources().getColor(R.color.circle_3));
                break;
            case 4:
                textView.setTextColor(context.getResources().getColor(R.color.circle_1));
                break;
            case 5:
                textView.setTextColor(context.getResources().getColor(R.color.circle_2));
                break;
            default:
                break;
        }
    }

    private void changeBackGround(LinearLayout layout, int type) {
        switch (type) {
            case 1:
                layout.setBackground(context.getResources().getDrawable(R.drawable.fund_circle_1));
                break;
            case 2:
                layout.setBackground(context.getResources().getDrawable(R.drawable.fund_circle_2));
                break;
            case 3:
                layout.setBackground(context.getResources().getDrawable(R.drawable.fund_circle_3));
                break;
            case 4:
                layout.setBackground(context.getResources().getDrawable(R.drawable.fund_circle_1));
                break;
            case 5:
                layout.setBackground(context.getResources().getDrawable(R.drawable.fund_circle_2));
                break;
            default:
                break;
        }
    }
}